package concurrent;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;


/**
 * await() 使当前线程等待，同时释放当前资源， 当其他线程中使用signal()或者signalAll()方法时，
 * 线程会重新获得锁并继续执行，或者当线程被中断时，也能跳出等待，与Object.wait()类似
 * awaitUninterruptibly() 与await()的区别是不会响应终端
 * signal() 唤醒一个等待的线程， signalAll()唤醒全部等待线程
 */
public class ReenterLockCondition implements Runnable {

    public static ReentrantLock lock = new ReentrantLock();
    public static Condition condition = lock.newCondition();


    @Override
    public void run() {
        try {
            lock.lock();
            System.out.println(1);
            condition.await();
            System.out.println("thread is going on");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReenterLockCondition tl = new ReenterLockCondition();
        Thread t1 = new Thread(tl);

        t1.start();
        Thread.sleep(2000);
        lock.lock();
        // 调用signal之前必须拿到锁， 调用玩之后需要释放锁
        condition.signal();
        lock.unlock();

    }


}
